import expression
from expression import expression
import sys
sys.path.append(sys.path[0] + "/../LLVM")
from llvmhelper import llvmHelper
from llvm import *
from commonRoutines import commonRoutines

class varRef(expression):
	def __init__(self, name):
		self.name = name
		self.eframe = None
	def evaluate(self, env):
		#print "\nvarRef: ", self.name,
		#print "env: \n", str(env), "\n"
		ret = env.getVarRef(self.name)
		#print " | post-eval: ", ret
		return ret
		
	def __str__(self):
		return "varRef(" + str(self.name) + ")"

	def traverse(self, helper):
		scope = helper.getCurrentScope()
		#sys.stderr.write("\nvarRef: name: " + self.name + "\n")
		var = helper.getVar(self.name)
		#sys.stderr.write("\nvarRef: self.eframe: " + str(var) + "\n")
		self.eframe = helper.getVar(self.name)
		
	def toLLVM(self, helper):
		helper.descend("varRef")
		c = commonRoutines(helper)
		
		helper.setLabel(helper.getLastLabel())
		#sys.stderr.write("varRef: " + self.name + " self.eframe: " + str(self.eframe) +"\n")
		if(self.eframe == "%this"):
			helper.ascend(self.eframe)
		elif(self.eframe != None):
			helper.ascend(c.fetchVar(self.eframe[1], self.eframe[2]))
		else:
			helper.ascend(helper.checkCurrentFunctionForArg(self.name))
		
#change this

